from django.contrib import admin
from .models import LogistQuestion, ChoiceOption


class ChoiceOptionInline(admin.TabularInline):
    model = ChoiceOption
    extra = 4
    fields = ['label', 'content', 'option_image']


@admin.register(LogistQuestion)
class LogistQuestionAdmin(admin.ModelAdmin):
    list_display = ['id', 'topic', 'question_type', 'score', 'suggested_time', 'stem_preview']
    list_filter = ['topic', 'question_type', 'score', 'suggested_time']
    search_fields = ['stem']
    inlines = [ChoiceOptionInline]
    
    def stem_preview(self, obj):
        return obj.stem[:50] + '...' if len(obj.stem) > 50 else obj.stem
    stem_preview.short_description = '题干预览'


@admin.register(ChoiceOption)
class ChoiceOptionAdmin(admin.ModelAdmin):
    list_display = ['question', 'label', 'content_preview']
    list_filter = ['question__topic', 'question__question_type']
    search_fields = ['content', 'question__stem']
    
    def content_preview(self, obj):
        if obj.content:
            return obj.content[:30] + '...' if len(obj.content) > 30 else obj.content
        return '无文本内容'
    content_preview.short_description = '选项内容预览' 